<template>
	<view class="content">
		<view class="swiper-block">
				<image src="https://job-1306862033.cos.ap-chengdu.myqcloud.com/miniprogram-static/home/jq-erq/banner.png" class="img"></image>
		</view>
		<view class="menu-block">
			<view class="title">
				请填写基本信息
			</view>
			<view class="form-item">
				<view style="display: flex;flex-direction: row;">
					<view style="color: #C10A11;">*</view>
					<view class="form-label">真实姓名</view>
				</view>
				<view class="form-content">
					<input v-model="form.name" maxlength="10" type="text" placeholder-class="inputPlaceholder"
						placeholder="请填写姓名" class="form-ctrl" />
					<view class="iconfont icon-gengduo1 arrow"></view>
				</view>
			</view>
			<view class="form-item">
				<view style="display: flex;flex-direction: row;">
					<view style="color: #C10A11;">*</view>
					<view class="form-label">手机号码</view>
				</view>
				<view class="form-content">
					<input v-model="form.phone" maxlength="11" type="number" placeholder-class="inputPlaceholder"
						placeholder="请填写手机号码" class="form-ctrl"/>
					<view class="iconfont icon-gengduo1 arrow"></view>
				</view>
			</view>
			<view class="form-item" style="border-bottom: none;" >
				<view class="form-content">
					<view style="display: flex;flex-direction: row;">
						<view style="color: #C10A11;">*</view>
						<view class="form-label">培训意向</view>
					</view>
					<uni-section title="培训意向" type="line" padding class="form-ctrl">
						<uni-data-picker placeholder="请选择培训种类" popup-title="请选择" :localdata="list" :map="listMap"
							v-model="form.specialityId" @change="onchange" @nodeclick="onnodeclick"
							@popupopened="onpopupopened" @popupclosed="onpopupclosed">
						</uni-data-picker>
					</uni-section>
				</view>
			</view>
		</view>
		<view class="step" @click="handleSave">
			提交
		</view>
	</view>
</template>

<script>
	import {
		getSpecialityByTrainId,
		userAddTrainPeopleApi,
	} from '@/api/train.js';
	import {
		validate
	} from '@/util/validate.js'
	export default {
		data() {
			return {
				list: [],
				listMap: {
					text: 'name',
					value: 'id',
					children: 'children'
				},
				form: {
					name: '',
					phone: '',
					trainId:null,
					trainType:null,
					specialityId:null,
					city:null,
				},
				type:1,
				tabActive: 0,
				labelIndex: 0,
				rules: {
					name: {
						name: '真实姓名',
						required: true
					},
					phone: {
						name: '手机号',
						required: true,
						validator: function(value, form, callback) {
							if (!/^1[3-9][0-9]{9}$/.test(value)) {
								callback('请输入正确的手机号')
							}
							callback();
						}
					},
					specialityId: {
						name: '培训种类',
						required: true
					},
				},
				id: null,
			}
		},
		methods: {
			
			onnodeclick(e) {
			},
			onpopupopened(e) {
			},
			onpopupclosed(e) {
			},
			onchange(e) {
				this.form.trainType = e.detail.value[0].value
				this.form.specialityId = e.detail.value[1].value
			},
			bindActive(index) {
				this.tabActive = index
				if (this.tabActive == 0) {
				}
				if (this.tabActive == 2) {
				}
			},
			navigate(url) {
				uni.navigateTo({
					url
				})
			},
			getSpecialityByTrainId() {
				uni.showLoading({
					title: '加载中',
					mask: true
				})
				getSpecialityByTrainId({
					trainId:this.form.trainId
				}).then(data => {
					uni.hideLoading()
					this.list = data
				}).catch(() => {
					uni.hideLoading()
				})
			},
			handleSave() {
				let form = JSON.parse(JSON.stringify(this.form));
				form.name = this.form.name
				form.trainId = this.form.trainId
				form.phone = this.form.phone
				form.trainType =this.form.trainType
				form.city = this.form.city
				if (validate(form, this.rules)) {
					uni.showLoading({
						title: '加载中',
						mask: true
					})
					userAddTrainPeopleApi(form).then((res) => {
						uni.hideLoading()
						this.showToast(res, () => {
							this.navigate('/page_other/submit/submit')
						})
					}).catch((res) => {
						uni.hideLoading()
						this.showToast('提交失败');
					})
				}
			}

		},
		onLoad(e) {
			//培训机构ID
			this.form.city = parseInt(e.city)
			this.form.trainId = parseInt(e.id)
			this.getSpecialityByTrainId()
		},
		onReachBottom() {
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #fafafa;
	}
	.form-content {
		display: flex;
		flex-direction: column;
		// align-items: center;
		
		.form-ctrl {
			height: 100rpx;
			line-height: 100rpx;
			flex-grow: 1;
		}
		
		.arrow {
			flex-grow: 0;
			color: #999;
			font-size: 24rpx;
		}
	}
	.selected-area{
		font-size: 32rpx;
	}
	.form-item1 {
		display: flex;
		flex-direction: column;
		border-bottom: 1px solid #efefef;
		padding-top: 30rpx;
	
		.form-label {
			font-size: 28rpx;
			color: #999;
		}
	
		
	}
	.swiper-block {
		width: 100%;
		height: 280rpx;
		box-shadow: 0 0 10px 0 rgba(0, 0, 0, 0.2);
		border-radius: 20rpx;
		.img{
			width: 100%;
			height: 100%;
		}
	}
	.content {
		width: 100%;
		padding: 30rpx;
		.step{
			width: 350rpx;
			height: 100rpx;
			display: flex;
			background-color: #1156FF;
			color: #fff;
			margin: 30rpx auto;
			border-radius: 60rpx;
			justify-content: center;
			align-items: center;
		}
		.menu-block {
			margin-top: 30rpx;
			background: #fff;
			border-radius: 12px;
			padding: 30rpx;
			.form-title {
				font-size: 36rpx;
				font-weight: bold;
			}
			
			.form-item {
				display: flex;
				flex-direction: column;
				border-bottom: 1px solid #efefef;
				padding-top: 30rpx;
			
				.form-label {
					font-size: 32rpx;
					color: #1A1A1A;
				}
			
				.form-content {
					display: flex;
					flex-direction: column;
					// align-items: center;
			
					.form-ctrl {
						height: 100rpx;
						line-height: 100rpx;
						flex-grow: 1;
					}
			
					.arrow {
						flex-grow: 0;
						color: #767676;
						font-size: 32rpx;
					}
				}
			}
			.title{
				font-weight: 900;
				font-size: 32rpx;
				line-height: 60rpx;
				width: 100%;
				color: #1A1A1A;
				overflow: hidden; 
				white-space: nowrap; 
				text-overflow: ellipsis;
			}
		}
	}
</style>